package mysql

import (
	"errors"
	"fmt"
)

type User struct {
	Id         uint `gorm:"column:id;primary_key" json:"id" form:"id"`
	Status     int  `gorm:"column:status" json:"status" form:"status"`
	Deleted    int  `gorm:"column:deleted" json:"deleted"`
	AddTime    int32 `gorm:"column:add_time" json:"add_time"`
	UpdateTime int32 `gorm:"column:update_time" json:"update_time"`
	Memo       string `gorm:"column:memo" json:"memo" form:"memo"`
	Account  string `gorm:"column:account" json:"account"`
	Password string `gorm:"column:password" json:"password"`
	Openid   string `gorm:"column:openid" json:"openid"`
	Type     string `gorm:"column:type" json:"type"`
	Acode    string `gorm:"column:acode" json:"acode"`
}

type Rs struct {
	Id int `json:"id"`
	Id_user int `json:"id_user"`
	Type int `json:"type"`
	Name string `json:"name"`
	Phone string `json:"phone"`
	IsVip int `json:"is_vip"`
	Status int `json:"status"`
	Active_time string `json:"active_time"`
}

type Online struct {
	Id int `json:"id"`
	Id_user int `json:"id_user"`
	Login_key string `json:"login_key"`
	Name string `json:"name"`
	Phone string `json:"phone"`
	Address string `json:"address"`
	Ip string `json:"ip"`
	Head_url string `json:"head_url"`
	Add_time string `json:"add_time"`
	Expiration_time string `json:"expiration_time"`
}


//这一步的意思是改表明的  默认表明是带s的
func (User) TableName() string {
	return "k_user"
}

func (model User) GetList(where []map[string]interface{}, page int, pageSize int) (map[string]interface{}, error) {

	if page <= 0 {
		return nil, errors.New("page 不正确")
	}
	if page == 1 {
		page = 0
	}
	result := map[string]interface{}{}
	db := Db
	num := 0

	offset := (page - 1) * pageSize

	for _, m := range where {
		db = db.Where(m["filed"].(string)+" "+m["exp"].(string)+" ?", m["val"])
	}

	fields := "k_user.id,k_user.type,k_user_info.name,k_user_info.phone,k_user.status,k_user_info.head_url,k_user_info.add_time,k_user_info.expiration_time,k_user_info.is_vip"

    var rss []Rs

	db = db.Table("k_user").Select(fields).Joins("left join k_user_info on k_user.id = k_user_info.id_user ")

	err := db.Count(&num).Limit(pageSize).Offset(offset).Scan(&rss).Error

	if err == nil {
		result["data"] = rss
		result["count"] = num
	}
	return result, err

}


func (model User) SetStatus(id int, status int) error {
	db := Db
	Role := User{}
	err := db.Model(&Role).Where("id = ?", id).Update("status", status).Error
	return err
}

func (model User) Del(id int) error {
	db := Db
	Role := User{}
	err := db.Where("id = ?", id).Delete(&Role).Error
	return err
}
func (model User) OnlineDel(id int) error {
	db := Db
	err := db.Table("k_user_online").Where("id_user = ?", id).Delete("").Error
	return err
}


func (model User) GetItemById(id int) (Rs, error) {

	db := Db
	var rs Rs
	err = db.Table("k_user_info").Where("id_user = ?",id).Scan(&rs).Error
	if err != nil {
		fmt.Println("查询错误", err)
		return rs, err
	}
	return rs, err
}

func (model User) GetOnList(where []map[string]interface{}, page int, pageSize int) (map[string]interface{}, error) {

	if page <= 0 {
		return nil, errors.New("page 不正确")
	}
	if page == 1 {
		page = 0
	}
	result := map[string]interface{}{}
	db := Db
	num := 0

	offset := (page - 1) * pageSize

	for _, m := range where {
		db = db.Where(m["filed"].(string)+" "+m["exp"].(string)+" ?", m["val"])
	}

	fields := "k_user_info.id,k_user_info.id_user,k_user_online.active_time,k_user_info.name,k_user_info.phone,k_user_info.is_vip,k_user_online.address,k_user_online.ip,k_user_online.login_key"

	var rss []Online

	db = db.Table("k_user_online").Select(fields).Joins("left join k_user_info on k_user_online.id_user = k_user_info.id_user ")

	err := db.Count(&num).Limit(pageSize).Offset(offset).Scan(&rss).Error

	if err == nil {


		result["data"] = rss
		result["count"] = num
	}

	return result, err

}